package com.hq.modeapp.adapter;

import java.util.ArrayList;

import com.hq.modeapp.R;
import com.hq.modeapp.adapter.NewsListViewAdapter.ViewHolder;
import com.hq.modeapp.bean.Ad_Bean;
import com.hq.modeapp.bean.News_Bean;
import com.hq.modeapp.manager.AppContext;
import com.hq.modeapp.ui.PhotoViewFlowActivity;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

/**
 * **********************************************************
 * 
 * @内容摘要 ：广告适配器
 * 
 * @作者 ：黄秋
 * @创建时间 ：2013-4-7 下午2:53:09
 * @历史记录 :
 * @日期 : 2013-4-7 下午2:53:09 修改人：黄秋
 * @描述 :
 *********************************************************** 
 */
public class Ad_Image_Adapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<Ad_Bean> list;
	private TextView tv_title;
	private AppContext appContext;

	public Ad_Image_Adapter(Context context, ArrayList<Ad_Bean> list,TextView tv_title) {
		mContext = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.list = list;
		this.tv_title = tv_title;
		this.appContext = (AppContext)(context.getApplicationContext());
	}

	@Override
	public int getCount() {
		if (getList().isEmpty()) {
			return 0;
		} else {
			return Integer.MAX_VALUE; // 返回很大的值使得getView中的position不断增大来实现循环
		}

	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = new ImageView(mContext);
			holder.tv_title=tv_title;
			holder.img_imgsrc=(ImageView) convertView;
			holder.img_imgsrc.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			holder.img_imgsrc.setScaleType(ScaleType.FIT_XY);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tv_title.setText(null);
		appContext.finalBitmap.display(holder.img_imgsrc, null);
		final Ad_Bean bean = getList().get(position % getList().size());
		holder.tv_title.setText(bean.getTitle());
		appContext.finalBitmap.display(holder.img_imgsrc, bean.getImgsrc());
		System.out.println("getView==的图片地址===" + bean.getImgsrc());
		holder.img_imgsrc.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				System.out.println("============");
				
				if(bean.getTag()!=null&&bean.getTag().equals("photoset")){
					Intent intent = new Intent();
					intent.setClass(mContext, PhotoViewFlowActivity.class);
					intent.putExtra("ID", bean.getUrl().split("\\|")[1]);
					mContext.startActivity(intent);
				}
				
				
				
				// adBean.setUrl("http://www.baidu.com");
				// adBean.setUrl(null);
//				try {
//					Intent intent = new Intent();
//					intent.setData(Uri.parse(adBean.getUrl()));
//					intent.setAction(Intent.ACTION_VIEW);
//					Main.this.startActivity(intent); // 启动浏览器
//				} catch (Exception e) {
//					e.printStackTrace();
//					Tools.getInstance().showTextToast(Main.this, "广告URL有误");
//				}

			}
		});
		return convertView;
	}

	public ArrayList<Ad_Bean> getList() {
		return list;
	}

	public void setList(ArrayList<Ad_Bean> list) {
		this.list.clear();
		this.list.addAll(list);
	}

	class ViewHolder {
		 TextView tv_title;
		 ImageView img_imgsrc;

	}
}